package com.hex.liyue.storemodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.utils.ValuesUtils;
import com.hex.liyue.storemodule.bean.FragmentStoreBean;
import com.hex.liyue.storemodule.bean.PayOrderDetailsResultBean;
import com.hex.liyue.storemodule.presenter.IStoreSureOrderView;
import com.hex.liyue.storemodule.presenter.StoreSureOrderPresenter;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 确认订单页面
 */
public class StoreSureOrderActivity extends BaseActivity implements IStoreSureOrderView {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;

    @Bind(R.id.iv_gift_icon)
    ImageView ivOrderIcon;
    @Bind(R.id.tv_sure_order_brand)
    TextView tvOrderBrand;
    @Bind(R.id.tv_sure_order_name)
    TextView tvOrderName;
    @Bind(R.id.tv_oeder_price)
    TextView tvOrderPrice;
    @Bind(R.id.tv_order_num)
    TextView tvOrderNum;
    @Bind(R.id.tv_submit_order_totalprice)
    TextView tvSubmitOrderTotalprice;
    private StoreSureOrderPresenter storeSureOrderPresenter;
    private String gift_id;
    private int gift_num;
    private FragmentStoreBean.DataBean dataBean;

    @Override
    protected void bindPresenter() {
        storeSureOrderPresenter = new StoreSureOrderPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        dataBean = (FragmentStoreBean.DataBean) getIntent().getSerializableExtra("dataBean");
        gift_id = dataBean.gift_id;
        gift_num = getIntent().getIntExtra("num", 0);

        Glide.with(this)
                .load(dataBean.gift_url)
                .error(R.drawable.default_error_image)
                .into(ivOrderIcon);
        tvOrderBrand.setText(dataBean.gift_brand);
        tvOrderName.setText(dataBean.gift_name);
        tvOrderPrice.setText("金额:￥" + dataBean.price);
        tvOrderNum.setText("数量:x " + gift_num);
        tvSubmitOrderTotalprice.setText("实付款：￥" + (dataBean.price * gift_num));
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_store_sure_order;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText(ValuesUtils.getString(R.string.orders_title));
    }


    @OnClick({R.id.iv_toobar_back, R.id.tv_submit_order})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.tv_submit_order://提交订单
                storeSureOrderPresenter.submitOrder(gift_id, gift_num);
                break;
        }
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void closeCurrentActivity(PayOrderDetailsResultBean payOrderDetailsResultBean) {
        //提交了订单  去支付
        Intent intent = new Intent(this, PaymentOrdersActivity.class);
        intent.putExtra("order_id", payOrderDetailsResultBean.data.order_id);
        //订单名字,
        intent.putExtra("name", dataBean.gift_name);
        // 订单描述
        intent.putExtra("desc", dataBean.gift_brand);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                setResult(RESULT_OK);
                finish();
            }
        } else if (resultCode == RESULT_CANCELED) {
            finish();
        }
    }
}
